package com.me.study.javaBasic.collection;

import org.junit.Test;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * 自定义 TreeMap 排序规则，并拼接成字符串
 *
 * @author ME
 * @date 2020/11/27
 */
public class TestLinkedHashMap {

    /**
     * 固定容量的 LinkedHashMap，超出指定容量后会自动删除
     */
    @Test
    public void fixedCapacity() {
        int maxSize = 4;
        LinkedHashMap<String, String> map = new LinkedHashMap<String, String>(4) {
            @Override
            protected boolean removeEldestEntry(Map.Entry<String, String> eldest) {
                return size() > maxSize;
            }
        };

        map.put("1", "abc");
        map.put("20", "abc");
        map.put("30", "abc");
        map.put("40", "abc");
        System.out.println(map);
        map.put("50", "abc");
        System.out.println(map);
    }

}
